/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.blog.city.checkthedep.managedbeans;

import com.blog.city.checkthedep.entities.Article;
import com.blog.city.checkthedep.sessionbeans.ArticleFacadeLocal;
import com.blog.city.checkthedep.sessionbeans.CommentFacadeLocal;
import com.blog.city.checkthedep.sessionbeans.TagFacadeLocal;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;


/**
 *
 * @author Breath
 */
@ManagedBean
@SessionScoped
public class OwnArticlesBean {

    private Article selectedArticle = new Article();
    private List<Article> ownArticles;
    private String tags = "";
    @EJB
    private CommentFacadeLocal commFac;
    @EJB
    private ArticleFacadeLocal articleFac;
    @EJB
    private TagFacadeLocal tagFac;
    @ManagedProperty(value = "#{loginBean}")
    private LoginBean loginBean;

    /**
     * 
     * @return
     */
    public LoginBean getLoginBean() {
        return loginBean;
    }

    /**
     * 
     * @param loginBean
     */
    public void setLoginBean(LoginBean loginBean) {
        this.loginBean = loginBean;
    }

    /**
     * 
     * @return
     */
    public List<Article> getOwnArticles() {
        return ownArticles;
    }

    /**
     * 
     * @param ownArticles
     */
    public void setOwnArticles(List<Article> ownArticles) {
        this.ownArticles = ownArticles;
    }

    /**
     * 
     * @return
     */
    public Article getSelectedArticle() {
        return selectedArticle;
    }

    /**
     * 
     * @param selectedArticle
     */
    public void setSelectedArticle(Article selectedArticle) {
        this.selectedArticle = selectedArticle;
    }

    /**
     * 
     * @return
     */
    public String getTags() {
        return tags;
    }

    /**
     * 
     * @param tags
     */
    public void setTags(String tags) {
        this.tags = tags;
    }

    /**
     * 
     */
    public void retrieveOwnArticles() {
        this.setOwnArticles(articleFac.findByRegUserId(loginBean.getCheckedUser()));
    }

    /**
     * 
     * @param id
     */
    public void retrieveSelectedArticle(Integer id) {
        this.selectedArticle = articleFac.find(id);
    }

    /**
     * 
     */
    public void setAuthor() {
        selectedArticle.setRegisteredUserId(loginBean.getCheckedUser());
    }

    /**
     * 
     */
    public void persistSelectedArticle() {
        if (selectedArticle != null) {
            setAuthor();
            /*
            String splitTags[] = tags.split(" ");
            List<Tag> tagsForArticle = new LinkedList<Tag>();
            for (int i = 0; i < splitTags.length; i++) {
            Tag tag = new Tag();
            tag.setName(splitTags[i]);
            if (tagFac.findTagByName(tag.getName()) == null) {
            tagsForArticle.add(tag);
            tagFac.create(tag);
            }
            }
            selectedArticle.setTagList(tagsForArticle);
             */
            articleFac.edit(selectedArticle);
        }
    }

    /**
     * 
     * @param id
     */
    public void deleteSelectedArticle(Integer id) {
        selectedArticle = new Article();
        this.retrieveSelectedArticle(id);
        if (articleFac.articleHasComments(selectedArticle)==false) {
            articleFac.remove(selectedArticle);
        } else {
            FacesMessage msg = null;
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Article", selectedArticle.getTitle() + " can not be deleted because it contains comments");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
        retrieveOwnArticles();
    }
}
